﻿using System;
using Microsoft.Practices.Composite.Modularity;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Composite.Wpf.Commands;
using Microsoft.Practices.Unity;
using WpfTwitterClient.Infrastructure;
using WpfTwitterClient.Infrastructure.Services;
using WpfTwitterClient.Modules.Authorization.Views.AccountView;
using WpfTwitterClient.Modules.Authorization.Views.StatusView;

namespace WpfTwitterClient.Modules.Authorization
{
    public class AuthorizationModule : IModule
    {
        private readonly IUnityContainer container;
        private readonly IRegionManager regionManager;
        private DelegateCommand<Object> myCommand;
        private ITwitterClient twitterClient;

        public AuthorizationModule(IUnityContainer container, IRegionManager regionManager, ITwitterClient twitterClient)
        {
            this.container = container;
            this.regionManager = regionManager;
            this.twitterClient = twitterClient;

            myCommand = new DelegateCommand<Object>(ShowAuthorizationEventHandler, ShowAuthorizationEventHandlerCanExecute);
        }

        public void Initialize()
        {
            this.RegisterViewsAndServices();

            var presenter = container.Resolve<IAccountPresenter>();

            IRegion mainRegion = this.regionManager.Regions[RegionNames.MainRegion];
            mainRegion.Add(presenter.View, "StatusView");
            mainRegion.Activate(presenter.View);
        }

        bool ShowAuthorizationEventHandlerCanExecute(object obj)
        {
            return true;
        }

        void ShowAuthorizationEventHandler(object obj)
        {
        }

        protected void RegisterViewsAndServices()
        {
            // TODO Should be singleton
            this.container.RegisterType<IAccountView, AccountView>();
            this.container.RegisterType<IAccountPresenter, AccountPresenter>();
        }
    }
}
